package com.starblink.imgsearch.result.ui.fragment

import android.animation.ObjectAnimator
import android.animation.ValueAnimator
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.animation.AccelerateDecelerateInterpolator
import androidx.core.view.updateLayoutParams
import com.starblink.android.basic.base.EmptyViewModel
import com.starblink.android.basic.base.fragment.BaseTVMFragment
import com.starblink.android.basic.ext.click
import com.starblink.android.basic.ext.visible
import com.starblink.android.basic.extension.dp2px
import com.starblink.android.basic.sensorsdata.extension.trackData
import com.starblink.android.basic.sensorsdata.spm.cfg.SpmElementDef
import com.starblink.android.basic.sensorsdata.spm.cfg.SpmPageDef
import com.starblink.android.basic.util.FlowCountDownTimer
import com.starblink.basic.ext.appScreenHeight

import com.starblink.imgsearch.databinding.FragmentImgSearchingBinding

/**
 *  Image Searching Fragment
 *  Create by yuanl at 2023/5/23
 **/
class ImgSearchingFragment : BaseTVMFragment<FragmentImgSearchingBinding, EmptyViewModel>() {

    private var scanAnimator: ObjectAnimator? = null
    private val downTimer = FlowCountDownTimer(time = 2)
    override fun initView(view: View) {
        super.initView(view)
//
//        viewBinding.btnCancel.updateLayoutParams<FrameLayout.LayoutParams> {
//            this.bottomMargin = CommUtils.getBottomStatusHeight()
//        }

        downTimer.start(callBack = {
        }, onFinish = {
            viewBinding.btnCancel.visible()
        })

        viewBinding.btnCancel.trackData(
            element = SpmElementDef.ScanCameraCancel,
            page = SpmPageDef.IMG_SEARCHING,
            onlyHit = true
        )
        viewBinding.btnCancel.setOnClickListener {
            requireActivity().finish()
        }
        viewBinding.vBackIcon.click {
            requireActivity().finish()
        }
        viewBinding.imgAnim.updateLayoutParams<ViewGroup.MarginLayoutParams> {
            topMargin = appScreenHeight / 8
        }
        scanAnimator = ObjectAnimator.ofFloat(
            viewBinding.imgAnim,
            "translationY",
            0f,
            appScreenHeight.shr(1).toFloat() + 100.dp2px(),
        ).apply {
            repeatMode = ValueAnimator.RESTART
            repeatCount = ValueAnimator.INFINITE
            duration = 2500L
            interpolator = AccelerateDecelerateInterpolator()
            start()
        }

    }

    override fun getViewBinding(
        inflater: LayoutInflater,
        container: ViewGroup?
    ): FragmentImgSearchingBinding = FragmentImgSearchingBinding.inflate(inflater, container, false)

    override fun startObserve() {

    }

    override fun initData() {

    }

    override fun onDestroy() {
        super.onDestroy()
        scanAnimator?.cancel()
        downTimer.cancel()
    }


    override fun onResume() {
        super.onResume()
        pageExposure(SpmPageDef.IMG_SEARCHING)
    }

    companion object {

        fun newInstance() = ImgSearchingFragment()

    }


}